package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IRenwuService;
import com.ruoyi.system.service.IZhuanjiaService;
import com.ruoyi.web.controller.tool.JsonResult;
import com.ruoyi.web.controller.tool.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IZhuanjiabiandongshenbaoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 专家变动申报Controller
 * 
 * @author dy
 * @date 2024-05-27
 */
@Controller
@RequestMapping("/system/biandongshenbao")
public class ZhuanjiabiandongshenbaoController extends BaseController
{


    @Autowired
    private IRenwuService renwuService;

    @Autowired
    private IZhuanjiaService zhuanjiaService;

    private String prefix = "system/biandongshenbao";

    private String ZJZXDeptId = RuoYiConfig.getZjzxdeptid();

    @Autowired
    private IZhuanjiabiandongshenbaoService zhuanjiabiandongshenbaoService;

    @RequiresPermissions("system:biandongshenbao:view")
    @GetMapping()
    public String biandongshenbao()
    {
        return prefix + "/biandongshenbao";
    }

    /**
     * 查询专家变动申报列表
     */
    @RequiresPermissions("system:biandongshenbao:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Zhuanjiabiandongshenbao zhuanjiabiandongshenbao)
    {
        startPage();
        List<Zhuanjiabiandongshenbao> list = zhuanjiabiandongshenbaoService.selectZhuanjiabiandongshenbaoList(zhuanjiabiandongshenbao);
        return getDataTable(list);
    }

    /**
     * 导出专家变动申报列表
     */
    @RequiresPermissions("system:biandongshenbao:export")
    @Log(title = "专家变动申报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Zhuanjiabiandongshenbao zhuanjiabiandongshenbao)
    {
        List<Zhuanjiabiandongshenbao> list = zhuanjiabiandongshenbaoService.selectZhuanjiabiandongshenbaoList(zhuanjiabiandongshenbao);
        ExcelUtil<Zhuanjiabiandongshenbao> util = new ExcelUtil<Zhuanjiabiandongshenbao>(Zhuanjiabiandongshenbao.class);
        return util.exportExcel(list, "专家变动申报数据");
    }

    /**
     * 新增专家变动申报
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存专家变动申报
     */
    @RequiresPermissions("system:biandongshenbao:add")
    @Log(title = "专家变动申报", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Zhuanjiabiandongshenbao zhuanjiabiandongshenbao)
    {
        return toAjax(zhuanjiabiandongshenbaoService.insertZhuanjiabiandongshenbao(zhuanjiabiandongshenbao));
    }

    /**
     * 修改专家变动申报
     */
    @RequiresPermissions("system:biandongshenbao:edit")
    @GetMapping("/edit/{shenbaoid}")
    public String edit(@PathVariable("shenbaoid") String shenbaoid, ModelMap mmap)
    {
        Zhuanjiabiandongshenbao zhuanjiabiandongshenbao = zhuanjiabiandongshenbaoService.selectZhuanjiabiandongshenbaoByShenbaoid(shenbaoid);
        mmap.put("zhuanjiabiandongshenbao", zhuanjiabiandongshenbao);
        return prefix + "/edit";
    }

    /**
     * 修改保存专家变动申报
     */
    @RequiresPermissions("system:biandongshenbao:edit")
    @Log(title = "专家变动申报", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Zhuanjiabiandongshenbao zhuanjiabiandongshenbao)
    {
        return toAjax(zhuanjiabiandongshenbaoService.updateZhuanjiabiandongshenbao(zhuanjiabiandongshenbao));
    }

    /**
     * 删除专家变动申报
     */
//    @RequiresPermissions("system:biandongshenbao:remove")
    @Log(title = "专家变动申报", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zhuanjiabiandongshenbaoService.deleteZhuanjiabiandongshenbaoByShenbaoids(ids));
    }

    @PostMapping("/custombiandonglist")
    @ResponseBody
    public JsonResult<?> custombiandonglist(@RequestParam("deptid")String ppDeptId, @RequestParam("zhuanjiamingcheng")String ppZhuanjiamingcheng,
                                            @RequestParam("biandongleixing")String ppBiandongleixing, @RequestParam("biandongriqikaishi")String ppBiandongriqikaishi,@RequestParam("biandongriqijieshu")String ppBiandongriqijieshu,
                                            @RequestParam("pageindex") int ppPageIndex,
                                            @RequestParam("pagesize") int ppPageSize){
        try{
            List<Map<String,Object>> list = new ArrayList<>();
            int mmCount = 0;
            if (ppDeptId.equals(ZJZXDeptId)){
                list = zhuanjiabiandongshenbaoService.selectZhuanjiaBiandongListCustom("",ppZhuanjiamingcheng,ppBiandongleixing,ppBiandongriqikaishi,ppBiandongriqijieshu,ppPageIndex,ppPageSize);
                mmCount = zhuanjiabiandongshenbaoService.countZhuanjiaBiandongListCustom("",ppZhuanjiamingcheng,ppBiandongleixing,ppBiandongriqikaishi,ppBiandongriqijieshu);
            }else{
                list = zhuanjiabiandongshenbaoService.selectZhuanjiaBiandongListCustom(ppDeptId,ppZhuanjiamingcheng,ppBiandongleixing,ppBiandongriqikaishi,ppBiandongriqijieshu,ppPageIndex,ppPageSize);
                mmCount = zhuanjiabiandongshenbaoService.countZhuanjiaBiandongListCustom(ppDeptId,ppZhuanjiamingcheng,ppBiandongleixing,ppBiandongriqikaishi,ppBiandongriqijieshu);
            }

            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("biandonglist", list);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            return JsonResult.getErrorResult(e.getMessage());
        }
    }

    @PostMapping("/save")
    @ResponseBody
    public JsonResult<?> save(Zhuanjiabiandongshenbao shenbao,
                              ZhuanjiabiandongshenbaoQuxiaozige quxiao,
                              ZhuanjiabiandongshenbaoChuguo chuguo,
                              ZhuanjiabiandongshenbaoLizhi lizhi,
                              ZhuanjiabiandongshenbaoShilian shilian,
                              ZhuanjiabiandongshenbaoTingfa tingfa,
                              ZhuanjiabiandongshenbaoQushi qushi,
                              ZhuanjiabiandongshenbaoHuifu huifu,
                              ZhuanjiabiandongshenbaoZhuanli zhuanli,
                              Zhuanjia zhuanjia,
                              String filepath,
                              @RequestParam("yuanyin")String yuanyin){
        try{
            String shenbaoid = shenbao.getShenbaoid();
            boolean ok = true;

            // 判断是否当前专家是否存在正在审核的事务
            List<Map<String,Object>> mmShiwuList = renwuService.existShenhezhongShiwuByZhuanjiaId(zhuanjia.getZhuanjiaid());
            if(mmShiwuList.size()>0){
                return JsonResult.getErrorResult("该专家有正在审核的变动，请勿重复申报");
            }

            //提交的专家变动
            String biandongleixing = shenbao.getBiandongleixing();
            //专家当前专家状态
            String zhuanjiazhuangtai = zhuanjiaService.selectZhuanjiaByZhuanjiaid(zhuanjia.getZhuanjiaid()).getZhuanjiazhuangtai();
            //99正常 7恢复 正常专家不能申报恢复
            if (zhuanjiazhuangtai.equals("99")&&biandongleixing.equals("7")){
                return JsonResult.getErrorResult("该专家当前状态正常，无法申报恢复发放津贴变动");
            }
            //99正常 7恢复 正常专家不能申报恢复
            if (!zhuanjiazhuangtai.equals("99")&&!biandongleixing.equals("7")){
                return JsonResult.getErrorResult("该专家为停发状态，无法申报除恢复发放津贴以外的变动");
            }

            if (shenbaoid == null) {
                if (!zhuanjiabiandongshenbaoService.addCustom(shenbao,quxiao,chuguo,lizhi,shilian,tingfa,qushi,huifu,zhuanli,zhuanjia,filepath,yuanyin)){
                    ok=false;
                }
            }else{
                if (!zhuanjiabiandongshenbaoService.modifyCustom(shenbao,quxiao,chuguo,lizhi,shilian,tingfa,qushi,huifu,zhuanli,zhuanjia,filepath,yuanyin)){
                    ok=false;
                }
            }

            return JsonResult.getSuccessResult(ok);
        } catch (Exception e) {
            return JsonResult.getErrorResult(e.getMessage());
        }
    }

    @PostMapping("/findcustomone")
    @ResponseBody
    public JsonResult<?> findcustomone(@RequestParam("shenbaoid") String ppShenbaoId){
        try{
            Map<String,Object> mmMap = zhuanjiabiandongshenbaoService.selectZhuanjiaBiandongCustom(ppShenbaoId);
            if (mmMap==null){
                return JsonResult.getErrorResult("该申报不存在");
            }

            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            return JsonResult.getErrorResult(e.getMessage());
        }
    }
}
